package com.jack.controller;

import java.sql.Timestamp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jack.mapper.income.IncomeMapper;
import com.jack.mapper.user.UserMapper;
import com.jack.model.Income;
import com.jack.model.User;

@RestController
@RequestMapping(value = "/income")
public class IncomeController {

	public static final String RESULT_SUCCESS = "success";
    public static final String RESULT_FAILED = "failed";

	
	@Autowired
	private IncomeMapper incomeMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@RequestMapping(value = "/addIncome/1")
	public String addIncome1(@RequestParam("name") String name, @RequestParam("amount") double  amount) {
		User user = new User();
		user.setName(name);
		userMapper.insert(user);
		
		
		Income income = new Income();
		income.setAmount(amount);
		income.setUserId(user.getId());
		income.setOpratedate(new Timestamp(System.currentTimeMillis()));
		incomeMapper.insert(income);
		
		return RESULT_SUCCESS;
	}
	
	@RequestMapping(value = "/addIncome/2")
	public String addIncome2(@RequestParam("name") String name, @RequestParam("amount") double  amount) {
		User user = new User();
		user.setName(name);
		userMapper.insert(user);
		
		
		Income income = new Income();
		income.setAmount(amount);
		income.setUserId(user.getId());
		income.setOpratedate(new Timestamp(System.currentTimeMillis()));
		incomeMapper.insert(income);
		
		return RESULT_SUCCESS;
	}
}
